/**
 * yvanui.grid
 * @author luoyifan
 * 2019-07-07 22:53:00
 */
"use strict";
(function () {

  if (!$.yvan) {
    console.error('$.yvan not loading');
    return;
  }

  $.extend($.yvan.component, {

    grid: function ($targetDOM, context, option) {
      if ($.yvan.beforeRender.call(this, $targetDOM, context, option) === false) {
        return;
      }

      var id = $.yvan.createId('grid');
      var $dom = $('<div id="' + id + '" yvan="grid"></div>');
      $targetDOM.append($dom);

      var source = {
        type: option.ajax.method,
        datatype: "json",
        cache: false,
        url: option.ajax.url,
        id: option.idField,
        beforeprocessing: function (root) {
          source.totalrecords = root.pagination.last;
        }
      };

      var dataAdapter = new $.jqx.dataAdapter(source, {
        contentType: 'application/json; charset=utf-8',
        loadError: function (xhr, status, error) {
          alert(error);
        },
        downloadComplete: function (root) {
          //计算序列号
          var i = (root.pagination.pageNo - 1) * root.pagination.pageSize + 1;
          root.data.forEach(function (r) {
            r.__NO__ = i++;
          });
          return {
            records: root.data
          };
        }
      });

      var columns = [{
        pinned: true, exportable: false, text: "",
        //columntype: 'number', //不要用这种方法，是从0开始的序号
        dataField: '__NO__', editable: false,
        cellsAlign: 'right', align: 'right',
      }].concat(option.columns);

      delete option.columns;

      option = $.extend({
        width: '100%',
        source: dataAdapter,
        //autoheight: true,     //不要自定适应数据行数来定义表格高度
        //showfilterrow: true,  //这句话会出异常
        showtoolbar: true,
        sortable: true,
        filterable: true,
        virtualmode: true,
        rendergridrows: function (obj) {
          return obj.data;
        },
        rendertoolbar: function (statusbar) {
          var $dom = $('<div yvan="toolbar"></div>');
          statusbar.append($dom);
          return toolbarRender($dom, context, option);
        },
        height: '100%',
        pageable: true,
        pagesizeoptions: ['10', '30', '50', '100'],
        pagerMode: 'advanced',
        pagerButtonsCount: 10,
        altrows: true,
        enabletooltips: true,
        columnsresize: true,
        sorttogglestates: 0,  //Disable column header click sort
        columns: columns
      }, option);

      delete option.component;
      delete option.ajax;
      delete option.idField;

      $dom.jqxGrid(option)
        .css({
          "border-style": 'none'
        })
        .on('bindingcomplete', function () {
          //console.log('bindingcomplete');
          //$(this).jqxGrid('autoresizecolumns', 'cells');
          $(this).jqxGrid('autoresizecolumn', '__NO__', 'all');
        })
        .on("columnclick", function (event) {
          $(this).jqxGrid('autoresizecolumn', event.args.datafield, 'all');
        });

      $.yvan.afterRender.call($dom[0], $targetDOM, context, option);
      return $dom;
    }
  });

})(jQuery);